<?php
namespace app\api\controller\v1\model\pet;

use think\Model;
use think\Db;


class Appointment extends model
{

//显示约宠信息			show_appointment
    public function getShowAppointment($appointment_id){
        $result = Db::table('ym_pet_appointment')->alias('a')->join('ym_pet b','a.pet_id = b.pet_id')->join('user_info c','a.user_id = c.user_id')->field(['c.user_name','c.user_icon','b.pet_icon','a.user_id','a.appointment_id','a.appointment_status','a.number_of_people','a.appointment_time','a.appointment_location'])->where('a.appointment_id',$appointment_id)->select();
        return $result;
    }



    //    判断宠粮是否足够约宠
    public function judgementPetFood($user_id){
        $res = Db::table('ym_pet_food')->field('pet_food')->where('user_id',$user_id)->select();
        $pet_food = $res[0]['pet_food'];

      return $pet_food;
    }

    //修改约宠开关数据
    public function setAppointmentSwitch($data = [])
    {
        $user_id = $data['user_id'];
        $switch = $data['switch'];
        $preg = '/^[1,0]\d*$/';
        if(!count($data) || !$user_id || !(preg_match($preg,$switch,$arr)))
        {
            //201:传入参数不合法
            return 201;
        }
        if($switch == 1)
        {
            $result = Db::table('user_info')->where('user_id',$user_id)->update(['appointment_or_not' => 0]);
        }
        else if($switch == 0)
        {
            $result = Db::table('user_info')->where('user_id',$user_id)->update(['appointment_or_not' => 1]);
        }
        //返回0或空为数据库问题
        if(!$result)
        {
            //202:数据库未查找到用户
            return 202;
        }
        //200:操作成功
        return 200;


    }


    /**
     * 从数据库获取当前约宠状态值
     * @param $user_id  用户id
     * @return $this|bool   返回数据库返回值
     */
    public function getAppointmentInfo($user_id)
    {
        try{
            $result = DB::name('user_info')->field('appointment_or_not')->where('user_id','=',$user_id)->find();
        }catch(\Exception $e) {
            return false;
        }
        return $result;
    }








}